package fts.ftsmonitoring.ftsTaggers;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import fts.ftsmonitoring.ftsTaggers.IAutoTagger;

public class TextContentKeywordTagger implements IAutoTagger
{

	HashMap<String, String> keywordsMap = new HashMap<String, String>();

	public TextContentKeywordTagger()
	{
		keywordsMap.put("heb", "hebrew");
		keywordsMap.put("hebrew", "hebrew");
		keywordsMap.put("birthday", "birthday");
		keywordsMap.put("java", "development");
		keywordsMap.put("c#", "development");
		keywordsMap.put(".net", "development");
		keywordsMap.put("framework", "development");
		keywordsMap.put("groovy", "development");
		keywordsMap.put("scala", "development");
		keywordsMap.put("c++", "development");
	}

	public String getDescription()
	{
		return "Tags text files using keywords";
	}

	public ArrayList<String> getTags(String fileName)
	{
		ArrayList<String> res = new ArrayList<String>();
		try
		{
			File f = new File(fileName);
			if (f.exists())
			{
				String contentType = Files.probeContentType(f.toPath());
				if (contentType != null)
				{
					if (contentType.startsWith("text"))
					{
						StringBuilder contents = new StringBuilder();

						BufferedReader input = new BufferedReader(new FileReader(f));
						try
						{
							String line = null;
							while ((line = input.readLine()) != null)
							{
								contents.append(line);
								contents.append(System.getProperty("line.separator"));
							}
						}
						finally
						{
							input.close();
						}

						String text = contents.toString();

						Iterator<String> iter = keywordsMap.keySet().iterator();
						while (iter.hasNext())
						{
							String key = iter.next();
							String val = keywordsMap.get(key);
							if (!res.contains(val) && text.indexOf(key) >= 0)
							{
								res.add(val);
							}
						}
					}
				}
			}
		}
		catch (Exception ex)
		{

		}
		return res;
	}
}
